<?php
/*
 * 电子定金合同上传接口
 */
require_once dirname(dirname(__FILE__)) . '/include/common.inc.php';
require_once dirname(dirname(__FILE__)) . '/include/econtract.class.php';
require_once INCLUDE_ROOT_PATH . 'file_server.class.php';

$act = isset($_REQUEST['act']) ? $_REQUEST['act'] : '';	//账户类型 0-个人，1-企业
$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : '';	//账户类型 0-个人，1-企业
$mobile = isset($_REQUEST['mobile']) ? $_REQUEST['mobile'] : '';	//手机号
$name = isset($_REQUEST['name']) ? $_REQUEST['name'] : '';	//名称、企业名称
$idcard = isset($_REQUEST['idcard']) ? $_REQUEST['idcard'] : '';	//身份证号、法人身份证号
$accountId = isset($_REQUEST['accountId']) ? $_REQUEST['accountId'] : '';	//生成的账户ID
$color = isset($_REQUEST['color']) ? $_REQUEST['color'] : '';	//印章颜色，1-红色，2-蓝色，3-黑色
$hText = isset($_REQUEST['hText']) ? $_REQUEST['hText'] : '';	//印章文字位置
$qText = isset($_REQUEST['qText']) ? $_REQUEST['qText'] : '';	//印章文字位置
$cust_id = isset($_REQUEST['cust_id']) ? $_REQUEST['cust_id'] : '';	//token验证
$filename = isset($_REQUEST['filename']) ? $_REQUEST['filename'] : '';	//token验证
$sealData = isset($_REQUEST['sealData']) ? $_REQUEST['sealData'] : '';	//token验证
$srcPdfFile = isset($_REQUEST['srcPdfFile']) ? $_REQUEST['srcPdfFile'] : '';	//token验证
$dstPdfFile = isset($_REQUEST['dstPdfFile']) ? $_REQUEST['dstPdfFile'] : '';	//token验证
$signType = isset($_REQUEST['signType']) ? $_REQUEST['signType'] : '';	//token验证
$posPage = isset($_REQUEST['posPage']) ? $_REQUEST['posPage'] : '';	//token验证
$posX = isset($_REQUEST['posX']) ? $_REQUEST['posX'] : '';	//token验证
$posY = isset($_REQUEST['posY']) ? $_REQUEST['posY'] : '';	//token验证
$key = isset($_REQUEST['key']) ? $_REQUEST['key'] : '';	//token验证
$token = isset($_REQUEST['token']) ? $_REQUEST['token'] : '';	//token验证
$upload_time = isset($_REQUEST['upload_time']) ? $_REQUEST['upload_time'] : '';	//pdf上传时间
if ($token != md5($mobile . 'econtractnewupload') || empty($upload_time)) //
{

	$result = false;
} else {
	if (!file_exists(PDF_ROOT_PATH) && !is_dir(PDF_ROOT_PATH)) {
		mkdir(PDF_ROOT_PATH);
	}
	//路径处理
	$year = date('Y', $upload_time);
	
	$tmp_path1 = PDF_ROOT_PATH . $year . '/';
	$tmp_path2 = PDF_ROOT_PATH . $year . '/' . $cust_id . '/';
	is_dir($tmp_path1) or mkdir($tmp_path1);
	is_dir($tmp_path2) or mkdir($tmp_path2);
	$file_dir = $tmp_path2;

	$filepath = $file_dir . $filename;
	$srcPdf = $file_dir . $srcPdfFile; // 原始文件
	$first = $file_dir . $dstPdfFile; //第一次签章后文件
	$File_server = new File_server();
	$econtract = new Econtract();
	switch ($act) {
		case 'addAccount':
			$result = $econtract->addAccount($type, $mobile, $name, $idcard);
			break;

		case 'addTemplateSeal':
			$res = $econtract->addTemplateSeal($type, $accountId, $color, $hText, $qText);
			if ($res['errCode'] == 0) {
				$result = $res['sealData'];
			} else {
				$result = $res;
			}

			break;
		case 'userSign'://用户签章
			$result = $econtract->userSign($accountId, $sealData, $srcPdf, $first, $fileName, $signType, $posPage, $posX, $posY, $key);

			break;

		case 'upload'://上传
			$result = $File_server->upload($file_dir);
			break;
		case 'view'://预览
			if (is_file($filepath)) {
				$result = $File_server->view($filepath);
				exit;
			} else {
				$result = $filepath; //$File_server->view($filepath);
			}
			break;
		default:
			$result = false;
			break;
	}
}


echo json_encode($result);
